package com.yun.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.UUID;


@Controller
public class upload {

    @RequestMapping(value = "/up")
    public String test() {
        return "up";
    }
    /**
     *   单文件上传
     * @param
     * @return
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public @ResponseBody String FileUpload(@RequestParam("file") MultipartFile file) {
        String filePath = "D:\\CaseUpload";
        try {
            InputStream inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (file.isEmpty()) {
            return "上传失败 文件为空.";
        }
        try {
            if (file.getOriginalFilename()!=null) {
                String prefix=file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
                File file2 = new File(filePath,UUID.randomUUID().toString().replaceAll("-", "")+prefix);
                if (!file2.getParentFile().exists()){
                    file2.getParentFile().mkdirs();
                }
                file.transferTo(file2);
            }
        } catch (Exception e) {
            return "上传失败   => " + e.getMessage();
        }
        return "上传成功";
    }
/*
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public @ResponseBody String handleFileUpload(HttpServletRequest request) {
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        String filePath = "D:\\CaseUpload";
        MultipartFile file;
        for (int i = 0; i < files.size(); ++i) {
            file = files.get(i);
            if (!file.isEmpty() && file.getOriginalFilename() != null) {
                String fileName = file.getOriginalFilename();
                File dest = new File(filePath, fileName);
                if (!dest.getParentFile().exists()) {
                    dest.getParentFile().mkdirs();
                }
                try {
                    file.transferTo(dest);
                } catch (Exception e) {
                    return "上传失败 " + i + " => " + e.getMessage();
                }
            } else {
                return "上传失败 " + i + " 文件为空.";
            }
        }
        return "上传成功";
    }*/
    /**
     *   文件下载
     * @param
     * @return
     */
    @RequestMapping(value = "/down", method = {RequestMethod.POST,RequestMethod.GET})
    public void FileDown(HttpServletResponse response){
        String filePath  = "D:\\CaseUpload\\111.txt";
        String fileName=filePath.substring(filePath.lastIndexOf("\\")+1);
        File file = new File(filePath);

            if (file.exists()) {
                response.reset();
                response.setContentType("application/octet-stream");
                response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
                byte[] buffer = new byte[1024];
                try {
                    FileInputStream fis = new FileInputStream(file);
                    BufferedInputStream bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

    }
}